package com.jack.concurrency;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;

/**
 * Created by zhang_j on 2019/12/13
 */
public class MyAtomic {

    public static void main(String[] args) {
        LongAdder sum = new LongAdder();
        new Thread(()->LongAdderIncrement(sum)).start();
        new Thread(()->LongAdderIncrement(sum)).start();
        new Thread(()->LongAdderIncrement(sum)).start();
        new Thread(()->LongAdderIncrement(sum)).start();
        new Thread(()->LongAdderIncrement(sum)).start();

        AtomicInteger num = new AtomicInteger(1);
        new Thread(()->AtomicIncrement(num)).start();
        new Thread(()->AtomicIncrement(num)).start();
        new Thread(()->AtomicIncrement(num)).start();
        new Thread(()->AtomicIncrement(num)).start();
        new Thread(()->AtomicIncrement(num)).start();
        //线程不安全
        int count = 1;
        new Thread(()->increment(count)).start();
        new Thread(()->increment(count)).start();
        new Thread(()->increment(count)).start();
        new Thread(()->increment(count)).start();
        new Thread(()->increment(count)).start();
    }

    public static void AtomicIncrement(AtomicInteger num){

        num.addAndGet(1);
        System.out.println("atomicIncrement"+num.intValue());
    }

    public static void increment(int num){
        num++;
        System.out.println("increment"+num);
    }

    public static void LongAdderIncrement(LongAdder longAdder){
        longAdder.add(1);
        System.out.println("longadder"+longAdder.intValue());
    }
}
